<!DOCTYPE html>
<%@ page import="java.util.*,rubix.model.*,rubix.controller.*,java.text.*" %>
<%@include file ="masterPage.jsp" %>
<html>
    <head>
        <title>KMP Fast East Pte Ltd - ERP Application</title>
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div id="page-wrapper">

            <div class="col-lg-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title"><i class="icon-file-text-alt"></i> Purchase Orders</h3><br>
                        <a href="createPurchaseOrder.jsp"><button type="button" class="btn btn-danger">Create Purchase Order</button></a>
                    </div>
                    <div class="panel-body">
                        
                        <div class="table-responsive">
                            <table class="table table-bordered table-hover table-striped tablesorter">
                                
                                    <thead>
                                         <tr class="success">
                                            <th>PO ID <i class="icon-sort"></i></th>
                                            <th>Supplier ID </th>
                                            <th>Purchase Date</th>
                                            <th>Total Amount</th>
                                            <th>View</th>
                                        </tr>
                                    </thead>
                                    <tbody>

                                            <%
                                                //User object is retrieved under the include file masterPage.jsp
                                                if(user == null) {
                                                    response.sendRedirect("loginMenu.jsp");
                                                }
                                                PoController poc = new PoController();
                                                ArrayList<Po> poList = poc.retrieveAll();
                                                for(int i=0; i<poList.size(); i++) {
                                                    Po po = poList.get(i);
                                                    String poId = po.getPoId();
                                                    String supplierId = po.getSupplierId();
                                                    String purchaseDate = po.getPurchaseDate();
                                                    String currency = po.getCurrency();
                                                    String itemId = po.getItemId();
                                                    String partId = po.getPartId();
                                                    String partPrice = po.getPartPrice();
                                                    double intPartPrice = 0;
                                                    int onOrderQuantity = po.getOnOrderQuantity();                                                    
                                                    try {
                                                        intPartPrice = Double.parseDouble(partPrice);
                                                    } catch(NumberFormatException e) { }
                                                    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                                                    DecimalFormat df2 = new DecimalFormat("##.00");
                                                    double tempTotalAmount = intPartPrice * onOrderQuantity;
                                                    double totalAmount = Double.valueOf(df2.format(tempTotalAmount));
                                            %>
                                        <tr>                                       
                                            <td><%=poId%></td>
                                            <td><%=supplierId%></td>
                                            <td><%=purchaseDate%></td>
                                            <td><%=totalAmount%></td>
                                            <td><a href="purchaseOrderDetails.jsp?poID=<%=poId%>"><input type="hidden" name="poID" value="<%=poId%>">View Selected Purchase Order <i class="icon-circle-arrow-right"></i></a></td>
                                        </tr>
                                            <%
                                                }          
                                            %>
                                    </tbody>
                            </table>
                            <ul class="pagination pagination-lg">
                                <li class="disabled"><a href="#">&laquo;</a></li>
                                <li class="active"><a href="purchaseOrder.jsp">1</a></li>
                                <li><a href="purchaseOrder.jsp">2</a></li>
                                <li><a href="purchaseOrder.jsp">3</a></li>
                                <li><a href="purchaseOrder.jsp">4</a></li>
                                <li><a href="purchaseOrder.jsp">5</a></li>
                                <li><a href="purchaseOrder.jsp">&raquo;</a></li>
                            </ul>
                            <div class="text-right">

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>